package com.neo.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author linhaicheng on 2019/05/27
 */
@Slf4j
public class FileUtil {


    public static InputStream getImageInputStream(String imageUrl) {

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(imageUrl);
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0");

        CloseableHttpResponse respond = null;
        InputStream result = null;
        try {
            respond = httpclient.execute(httpGet);
            log.info("statusCode:{}", respond.getStatusLine().getStatusCode());
            HttpEntity entity = respond.getEntity();
            result = entity.getContent();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    private void localTest(InputStream imageInputStream) {
        try {
            ImageIO.write(ImageIO.read(imageInputStream), "png", new File("C:\\intellij\\idea\\spring-boot-examples\\spring-boot-fastDFS\\src\\main\\resources\\1.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
